package com.situ.company.web220111.project.servlet;

import com.situ.company.web220111.project.model.ProjectModel;
import com.situ.company.web220111.project.service.IProjectService;
import com.situ.company.web220111.project.service.impl.ProjectServiceImpl;
import com.situ.company.web220111.util.FmtRequest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/ProjectServlet")
public class ProjectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    IProjectService service = new ProjectServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object res = null;
        switch (req.getParameter("action")) {
            case "page"://分页
                res = page(req);
                break;
            case "list"://查出所有项目返回给添加绩效表
                res = list(req);
                break;
            case "add":
                res = add(req);
                break;
            case "del":
                res = del(req);
                break;
            case "updName":
                res = updName(req);
                break;
            case "get":
                res = get(req);
                break;
            case "updTime":
                res = updTime(req);
                break;

            default:
                break;
        }
        FmtRequest.write(resp.getWriter(), res);

    }

    private List<ProjectModel> list(HttpServletRequest req) {
        return service.selectList(FmtRequest.parseModel(req, ProjectModel.class));
    }

    private String updTime(HttpServletRequest req) {
        return service.update(FmtRequest.parseModel(req, ProjectModel.class));
    }

    private ProjectModel get(HttpServletRequest req) {
        return service.selectModel(FmtRequest.parseModel(req, ProjectModel.class));
    }

    private String updName(HttpServletRequest req) {
        return service.update(FmtRequest.parseModel(req, ProjectModel.class));
    }

    private String del(HttpServletRequest req) {
        return service.delete(FmtRequest.parseModel(req, ProjectModel.class));
    }

    private String add(HttpServletRequest req) {
        return service.insert(FmtRequest.parseModel(req, ProjectModel.class));
    }


    private Map<String, Object> page(HttpServletRequest req) {
        ProjectModel model = new ProjectModel();
        model.setCode(req.getParameter("code"));
        model.setName(req.getParameter("name"));
        model.setPageIndex(Integer.parseInt(req.getParameter("pageIndex")));
        model.setPageLimit(Integer.parseInt(req.getParameter("pageLimit")));
        model.setPageOn(true);
        List<ProjectModel> list = service.selectList(model);
        Integer count = service.selectCount(model);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return map;
    }


}
